<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    @ok="handleSubmit"
    :title="getTitle"
    width="1000px"
  >
    <div class="pt-3px pr-3px">
      <dataStorageForm @register="registerForm" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref, nextTick } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { dataStorageForm } from '/@/components/Form/index';
  import { init } from './data';
  import { addStorage, updateStorage } from '/@/api/sys/storeData';
  export default defineComponent({
    components: { BasicModal, dataStorageForm },
    props: {
      method: { type: String, defaultValue: 'add' },
      formData: { type: Object },
    },
    emits: ['success'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const { registerForm, setFieldsValue, resetFields, validate, switchType } = init();
      const [register, { setModalProps, closeModal }] = useModalInner((data) => {
        resetFields();
        data && onDataReceive(data);
      });
      //初始化弹框
      function onDataReceive(data) {
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;
        if (unref(isUpdate)) {
          switchType(data.rowdata.storage_type);
          nextTick(() => {
            if (!data.rowdata.interface_type) {
              data.rowdata.interface_type = 1;
            }
            setFieldsValue(data.rowdata);
          });
        } else {
          switchType(null);
          setTimeout(() => {
            setFieldsValue({
              interface_type: 1,
            });
          }, 1000);
        }
      }
      const getTitle = computed(() => (!unref(isUpdate) ? '新增数据存储' : '编辑数据存储'));
      async function handleSubmit() {
        try {
          const data = await validate();
          let obj = {
            id: undefined,
            config: {},
            storage_type: '',
            name: '',
            describe: '',
            principal: '',
            phone: '',
            disable: 1,
          };
          console.log('是否禁用' + data.disable);
          obj.config = data;
          obj.id = data.id;
          obj.storage_type = data.storage_type;
          obj.name = data.name;
          obj.describe = data.describe;
          obj.principal = data.principal;
          obj.phone = data.phone;
          obj.disable = data.disable ? 0 : 1;
          delete obj.config.storage_type;
          delete obj.config.name;
          delete obj.config.describe;
          delete obj.config.principal;
          delete obj.config.phone;
          setModalProps({ confirmLoading: true });
          if (unref(isUpdate)) {
            await updateStorage(obj);
          } else {
            await addStorage(obj);
          }
          closeModal();
          emit('success', { isUpdate: unref(isUpdate) });
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }
      return { register, getTitle, registerForm, handleSubmit };
    },
  });
</script>
